package com.sunwayworld.basemodule.samplemanage.location.validators;

import com.sunwayworld.basemodule.samplemanage.location.bean.LimsLocationBoardBean;
import com.sunwayworld.basemodule.samplemanage.sample.bean.LimsSampleBean;
import com.sunwayworld.basemodule.samplemanage.sample.service.LimsSampleService;
import com.sunwayworld.framework.i18n.I18nHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.CollectionUtils;
import com.sunwayworld.framework.validator.data.DataValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsLocationBoardDeleteValidator implements DataValidator {

    @Autowired
    private LimsSampleService sampleService;

    @Override
    public boolean doValid(Object... args) {
        RestJsonWrapperBean wrapper = (RestJsonWrapperBean) args[0];
        List<LimsLocationBoardBean> delete = wrapper.parse(LimsLocationBoardBean.class);
        List<Long> ids = delete.stream().map(LimsLocationBoardBean::getId).collect(Collectors.toList());
        List<LimsSampleBean> sampleBeans = sampleService.selectListByFilter(SearchFilter.instance().match("boardId", ids).filter(MatchPattern.OR));
        if(!CollectionUtils.isEmpty(sampleBeans)){
            this.addConstraintViolation(I18nHelper.getMessage("T_LIMS_LOCATION_BOARD.DELETE_SAMPLE_NOT_EMPTY"));
            return false;
        }
        return true;
    }
}
